#include "capturewrapper.h"

CaptureWrapper::CaptureWrapper(QObject *parent) : QObject(parent)
{
    m_captureProcess = new QProcess(this);

    QString program = "/usr/bin/capture";
    QStringList arguments;
    //arguments << "--viewfinder";

    m_captureProcess->start(program);

    qDebug() << program;
    //qDebug() << arguments;

    m_captureProcess->waitForStarted(-1);

    qDebug() << "bytes written: " << m_captureProcess->write("start\n");
    qDebug() << "bytes written: " << m_captureProcess->write("flash off\n");
    qDebug() << "bytes written: " << m_captureProcess->write("viewfinder on\n");
    qDebug() << "bytes written: " << m_captureProcess->write("size small\n");

    m_captureProcess->waitForReadyRead();
    qDebug() << m_captureProcess->readAll();
}

CaptureWrapper::~CaptureWrapper()
{
    m_captureProcess->write("viewfinder on\n");
    m_captureProcess->write("quit\n");
    qDebug() << m_captureProcess->readAll();
    m_captureProcess->waitForFinished(-1);
    qDebug() << "~CaptureWrapper";
}

void CaptureWrapper::setFlash(bool flash)
{
    QByteArray out;
    if (flash)
      out.append("flash on");
    else
      out.append("flash off");
    out.append("\n");
    qDebug() << "wrapper start flash" << flash;
    m_captureProcess->write(out);
    m_captureProcess->waitForReadyRead();
    qDebug() << m_captureProcess->readAll();
    qDebug() << "stop flash" << flash;
}

void CaptureWrapper::setZoom(QString zoom)
{
    QByteArray out;
    out.append("zoom ");
    out.append(zoom);
    out.append("\n");
    qDebug() << "wrapper start zooming" << zoom;
    m_captureProcess->write(out);
    m_captureProcess->waitForReadyRead();
    qDebug() << m_captureProcess->readAll();
    qDebug() << "stop zooming" << zoom;
}

void CaptureWrapper::takeSnapshot(QString filename)
{
    QByteArray out;
    out.append("capture ");
    out.append(filename);
    out.append("\n");
    qDebug() << "wrapper start taking" << filename;
    m_captureProcess->write(out);
    m_captureProcess->waitForReadyRead();
    qDebug() << m_captureProcess->readAll();
    qDebug() << "stop taking" << filename;
}
